
NGINX_PATH=`echo $(cd "$(dirname "$0")"; pwd)`
NGINX_WWW_PATH=/var/www

InstallNginxService ()
{
    if [ -e /etc/nginx.cnf ] ; then
        /bin/mv -f /etc/nginx.cnf /etc/nginx.backup.cnf
    fi

    if [ ! -d "${NGINX_PATH}/logs/" ]; then
        mkdir ${NGINX_PATH}/logs/
    fi

    if [ ! -d "${NGINX_WWW_PATH}" ]; then
        mkdir -p ${NGINX_WWW_PATH}
    fi

    if [ -d "${NGINX_PATH}/www/" ]; then
        /bin/rm -rf ${NGINX_PATH}/www
    fi

    if [ -d "${NGINX_WWW_PATH}" ]; then
        ln -s ${NGINX_WWW_PATH} ${NGINX_PATH}/www
    else
        ln -s ${NGINX_PATH}/html ${NGINX_PATH}/www
    fi

    chmod -R 777 ${NGINX_PATH}/logs
    chmod -R 755 ${NGINX_PATH}/www
    chmod +x ${NGINX_PATH}/sbin/nginx
    sed -i "s%ABNGX%${NGINX_PATH}%g" ${NGINX_PATH}/nginx.service
    systemctl daemon-reload
    systemctl enable ${NGINX_PATH}/nginx.service
    systemctl start nginx.service
}

InstallNginxService